/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function Lookup() {
    this.javaClass = "limousine.vo.Lookup";
    this.tableId = 0;
    this.lookupId = "";
    this.lookupName = "";
    this.lookupValue = "";
    this.lookupDescription = "";
    this.lookupGroupId = "";
    this.lookupOrder = 0;   
}
function LookupManager() {
    
}
function openLookup() {
    var body = $d("_body");
    body.innerHTML = requestPage("lookup.main.jsp");
}
function edit(id) {
    try {
        addProcessing('Please wait...');
        formEntry();
        document.getElementById("fieldSetLabel").textContent="Lookup Edit";
        var o = service.findById(id);
        $d("tableId").value = o.tableId;
        $d("lookupId").value = o.lookupId;
        $d("lookupName").value = o.lookupName;
        $d("lookupValue").value = o.lookupValue;
        $d("lookupDescription").value = o.lookupDescription;
        $d("groupId").value = o.lookupGroupId;
        $d("orderId").value = o.lookupOrder;
        $d("save").setAttribute("onclick","save()");
    } catch(err) {
        alert("edit err:"+err);
    }
    clearAllMsg();
}
function switchProcess(type,msg,method){
    if(type == 0){//fail
        addSysMsg(0,msg);
    }else{
        addProcessing(msg);
        setTimeout(method,2500);        
    }    
}
function save() {
    try {
        addProcessing('Please wait...');
        var o = new Lookup();
        o.tableId = parseInt($d("tableId").value);
        o.lookupId = $d("lookupId").value;
        o.lookupName = $d("lookupName").value;
        o.lookupValue = $d("lookupValue").value;
        o.lookupDescription = $d("lookupDescription").value;
        o.lookupGroupId = $d("groupId").value;
        o.lookupOrder = parseInt($d("orderId").value);
        if( verifyLookup() == true ) {
            addProcessing();
            var ret = service.update(o);
            if( ret == "true" ) {
                switchProcess(1,"Save completed.","formSearch()");
            } else {
                switchProcess(0,"Save fail.","");
            }
        }
    } catch(err) {
        alert("save err:"+err);
    }
    clearAllMsg();
    searchLookup(0);
}
function remove(id) {
    try {
        addProcessing('Please wait...');
        service.remove(id);
        searchLookup(0);
    } catch(err) {
        alert("remove err:"+err);
    }
    clearAllMsg();
}
function searchLookup(offset) {
    try {
        addProcessing('Please wait...');
        var name = ($d("lookupNameSearch").value == null ) ? "": $d("lookupNameSearch").value;
        var group = ($d("lookupGroupSearch").value == null ) ? "": $d("lookupGroupSearch").value;
        name = (name == "" ) ? "*": name;
        group = (group == "" ) ? "*": group;
        
        var tokens = name+"_comma_"+group;
        var ls = service.search(offset, tokens);
        var scount = service.getCount( tokens);
        //alert("count:"+ scount);
        var jTable = new JTable();
        jTable.setTableId("tableData");
        jTable.setColumns(["Name", "Value", "Description", "Group Id"]);
        jTable.setFields(["lookupName","lookupValue","lookupDescription", "lookupGroupId"]);
        jTable.setEditFunction("edit");
        jTable.setDeleteFunction("remove");
        jTable.setAlignment("cols[3]='center';cols[4]='center'");
        jTable.setDataId("tableId");
        jTable.setCount(scount);        
        jTable.setOffset(offset);
        jTable.setDataPageSize(20);
        jTable.setPageLink("searchLookup");
        jTable.setTableHeader();
        jTable.loadData(ls);
    } catch(err) {
        alert("search err:"+err);      
    }
    clearAllMsg();
}
function insert() {
    try {
        addProcessing('Please wait...');
        var o = new Lookup();
        o.lookupId = $d("lookupId").value;
        o.lookupName = $d("lookupName").value;
        o.lookupValue = $d("lookupValue").value;
        o.lookupDescription = $d("lookupDescription").value;
        o.lookupGroupId = $d("groupId").value;
        o.lookupOrder = $d("orderId").value;
        if( verifyLookup() == true ) { 
            addProcessing();
            var ret = service.save(o);
            if( ret == "true" ) {
                switchProcess(1,"Add completed.","formSearch()");
            } else {
                switchProcess(0,"Add fail.","");
            }
        }
    } catch(err) {
        alert("insert err:"+err);
    }
    clearAllMsg();
    searchLookup(0);
}
        
function setInsert() {
    try {
        formEntry();
        var searchd = $d("lookupNameSearch").value;
        $d("actionForm").reset();
        $d("lookupId").value = getRequestId();
        $d("lookupNameSearch").value = searchd;
        $d("save").setAttribute("onclick","insert()");
    } catch(err) {
        alert("setInsert err:"+err);
    }
}
         
function formEntry() {
    clearAllMsg();
    document.getElementById("fieldSetLabel").textContent="Lookup Add";
    try {
        $d("formSearch").style.visibility = "hidden";
        var formEntry = $d("formEntry");
        formEntry.style.visibility = "visible";
        var panel = $d("panel");
        formEntry.style.top = panel.style.top;
        formEntry.style.left = panel.style.left;
    } catch(err) {
        alert("formEntry err:"+err);
    }
}
function formSearch() {
    clearAllMsg();
    document.getElementById("fieldSetLabel").textContent="Lookup Search";    
    try {
        $d("formEntry").style.visibility = "hidden";
        var search = $d("formSearch");
        search.style.visibility = "visible";
        var panel = $d("panel");
        search.style.top = panel.style.top;
        search.style.left = panel.style.left;
    } catch(err) {
        alert("formSearch err:"+err);
    }
}
function getRequestId() {
    
    var id = service.requestId();
    //alert("id:"+ id);
    return id;
}
function verifyLookup() {
    try {
        var v = new Validation();
        v.addField("name='lookupName',label='Name',min=1,max=50,type='string'");
        v.addField("name='lookupValue',label='Value',min=1,max=50,type='string'");
        v.addField("name='lookupDescription',label='Description',min=4,max=255,type='string'");
        v.addField("name='groupId',label='Group Id',min=4,max=25,type='string'");
        v.addField("name='orderId',label='Order Id',min=0,max=5,type='string'");
        var str = v.execute();
        if ( str.length == 0 ) {
            return true;
        } else {
            addSysMsg(0,str);
            return false;
        }
    } catch(err) {
        alert(err);
    }
}